﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using Autodesk.Revit.DB;
using Autodesk.Revit.Exceptions;
using Autodesk.Revit.UI;
using YArchitech.Controls;
using YArchitech.LIB;
using YArchitech.Revit;
using YJKCodeBase.WinForm;

namespace FetchDataCompare
{
	public partial class FetchDataCompareForm : System.Windows.Forms.Form
	{
		public FetchDataCompareForm()
		{
			this.InitializeComponent();
		}

		public FetchDataCompareForm(ExternalCommandData revit, WindowHandle revitMainHandle)
		{
			this.InitializeComponent();
			this.m_Revit = revit;
			this.m_RevitMainHandle = revitMainHandle;
		}

		private bool IsTwoDocSame(string srcDocPath, string desDocPath)
		{
			return srcDocPath == desDocPath;
		}

		private bool GetSameViews(List<Autodesk.Revit.DB.View> srcViews, List<Autodesk.Revit.DB.View> desViews, ref List<Autodesk.Revit.DB.View> newSrcViews, ref List<Autodesk.Revit.DB.View> newDesViews)
		{
            foreach (Autodesk.Revit.DB.View view in srcViews)
			{
				string a = "";
				try
				{
					a = view.GetParameter("视图分类-父").AsString();
				}
				catch (Exception)
				{
					continue;
				}
                foreach (Autodesk.Revit.DB.View view2 in desViews)
				{
					string b = "";
					try
					{
						b = view2.GetParameter("视图分类-父").AsString();
					}
					catch (Exception)
					{
						continue;
					}
					if (view.ViewType == view2.ViewType && view.ViewName == view2.ViewName && a == b)
					{
						newSrcViews.Add(view);
						newDesViews.Add(view2);
						break;
					}
				}
			}
			return true;
		}

		private void btnSure_Click(object sender, EventArgs e)
		{
			if (this.txtLoadFilePath.Text.Trim() == "")
			{
				YJKMessageBox.Show("请选择被比对的文件！");
				return;
			}
			Document document = null;
			try
			{
				document = this.m_Revit.Application.Application.OpenDocumentFile(this.txtLoadFilePath.Text.Trim());
			}
			catch (CorruptModelException)
			{
				YJKMessageBox.Show("选择的比对文件是在较高版本中保存的，因此无法在本版本中打开！");
				return;
			}
			catch (Autodesk.Revit.Exceptions.OperationCanceledException)
			{
				YJKMessageBox.Show("低版本文件升级操作被取消");
				return;
			}
			Document document2 = this.m_Revit.Application.ActiveUIDocument.Document;
			if (document2.IsModified)
			{
				document.Dispose();
				YJKMessageBox.Show("当前文档发生编辑且未被保存，请保存后继续该操作");
			}
			else
			{
				if ("" == document2.PathName)
				{
					document.Dispose();
					YJKMessageBox.Show("选择的被比较文件为未保存的项目，请保存后在继续该操作！");
					return;
				}
				if (this.IsTwoDocSame(document2.PathName, this.txtLoadFilePath.Text.Trim()))
				{
					document.Dispose();
					YJKMessageBox.Show("选择的两版文件为同一文件！");
					return;
				}
				DocumentManger documentManger = null;
				DocumentManger documentManger2 = null;
				try
				{
                    documentManger = new DocumentManger(document, "", false, (ViewType)1);
                    documentManger2 = new DocumentManger(document2, "", false, (ViewType)1);
				}
				catch (Exception)
				{
					YJKMessageBox.Show("两对比文件采用的非Revit样板做的模型，不予对比重新选择模型");
					return;
				}
				List<Autodesk.Revit.DB.View> views = new List<Autodesk.Revit.DB.View>();
				List<Autodesk.Revit.DB.View> views2 = new List<Autodesk.Revit.DB.View>();
				this.GetSameViews(documentManger.Views, documentManger2.Views, ref views, ref views2);
				documentManger.Views = views;
				documentManger2.Views = views2;
				documentManger.InitGeoObjDataByDoc();
				documentManger2.InitGeoObjDataByDoc();
				FetchDataCompare fetchDataCompare = new FetchDataCompare(documentManger, documentManger2);
				fetchDataCompare.DoCompare();
				AddBoundaryRequest addBoundaryRequest = new AddBoundaryRequest();
				addBoundaryRequest.DarwBoundary(fetchDataCompare.LstReportData, documentManger, documentManger2);
				this.m_ReportDataListForm = new ReportDataListForm(fetchDataCompare.LstReportData, this.m_Revit, documentManger, documentManger2);
				this.m_ReportDataListForm.Show(this.m_RevitMainHandle);
				base.Close();
				int wMsg = 7;
				FetchDataCompareForm.SendMessage(this.m_RevitMainHandle.Handle, wMsg, IntPtr.Zero, IntPtr.Zero);
				return;
			}
		}

		[DllImport("user32.dll", EntryPoint = "SendMessageA")]
		public static extern int SendMessage(IntPtr hwnd, int wMsg, IntPtr wParam, IntPtr lParam);

		private void btnCancel_Click(object sender, EventArgs e)
		{
			base.Close();
		}

		private void btnLoadFile_Click(object sender, EventArgs e)
		{
			try
			{
				OpenFileDialog openFileDialog = new OpenFileDialog();
				openFileDialog.Filter = "Revit文件 | *.rvt";
				openFileDialog.ShowDialog();
				this.txtLoadFilePath.Text = openFileDialog.FileName;
			}
			catch (Exception)
			{
				this.txtLoadFilePath.Text = "";
				YJKMessageBox.Show("选择对比文件失败");
			}
		}

		private void FetchDataCompareForm_Load(object sender, EventArgs e)
		{
			//base.Icon = Resource.YArchitechIcon;
		}

		private void FetchDataCompareForm_KeyPress(object sender, KeyPressEventArgs e)
		{
			if (e.KeyChar == '\u001b')
			{
				base.Close();
			}
		}

		private ExternalCommandData m_Revit;

		private WindowHandle m_RevitMainHandle;

		private ReportDataListForm m_ReportDataListForm;
	}
}
